package ControlLayer;

import java.util.ArrayList;
import java.util.Date;
import DBLayer.DbConnection;
import DBLayer.DbInvoice;
import DBLayer.IfDbInvoice;
import ModelLayer.Invoice;

public class InvoiceCtrl
{
	public ArrayList<Invoice> findAllInvoices()
    {
        IfDbInvoice dbInvoice = new DbInvoice();
        ArrayList<Invoice> allInvoices = new ArrayList<Invoice>();
        allInvoices = dbInvoice.getAllInvoices(false);
        return allInvoices;
    }
    public Invoice findInvoicesByPaymentDate(Date paymentDate)
    {
    	IfDbInvoice dbInvoice = new DbInvoice();
        return dbInvoice.findInvoicesByPaymentDate(paymentDate, true);
    }
    public void insertInvoice(Date paymentDate)
    {
    	Invoice invoiceObject = new Invoice();
    	invoiceObject.setPaymentDate(paymentDate);
 
        try
        {
            DbConnection.startTransaction();
            DbInvoice dbInvoice = new DbInvoice();
            dbInvoice.insertInvoice(invoiceObject);
            DbConnection.commitTransaction();
        }
        catch(Exception e)
        {
            DbConnection.rollbackTransaction();
        }
    }

    public int updateInvoice(Date paymentDate)
    {
    	IfDbInvoice dbInvoice = new DbInvoice();
        Invoice invoice = new Invoice();
        invoice.setPaymentDate(paymentDate);
        return dbInvoice.updateInvoice(invoice);
    }
    
    public int deleteInvoice(Date paymentDate)
    {
    	IfDbInvoice dbInvoice = new DbInvoice();
    	return dbInvoice.deleteInvoice(paymentDate);
    }
}